www.gusucode.com > VC 自编SQL数据库(服务端+客户端) > VC 自编SQL数据库(服务端+客户端)/code/www.NewXing.com/sql/Client/client.cpp

    // client.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "client.h"

#include "MainFrm.h"
#include "ChildFrm.h"
#include "clientDoc.h"
#include "clientView.h"
#include "HelpDoc.h"
#include "HelpView.h"
#include "SelDoc.h"
#include "SelView.h"

#include "Msg.h"
#include "LogDlg.h"
#include "Error.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// Global function
void Message( LPCTSTR text )
{
	CMainFrame* pMainFrm = ( CMainFrame* )AfxGetMainWnd();
	pMainFrm->m_TabBar.InsertText( text );
}

/////////////////////////////////////////////////////////////////////////////
// CClientApp

BEGIN_MESSAGE_MAP(CClientApp, CWinApp)
	//{{AFX_MSG_MAP(CClientApp)
	ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
	ON_COMMAND(ID_CONNECT, OnConnect)
	ON_UPDATE_COMMAND_UI(ID_CONNECT, OnUpdateConnect)
	ON_COMMAND(ID_DISCONNECT, OnDisconnect)
	ON_UPDATE_COMMAND_UI(ID_DISCONNECT, OnUpdateDisconnect)
	ON_COMMAND(ID_HELP_ME, OnHelpMe)
	ON_COMMAND(ID_FILE_NEW, OnFileNew)
	//}}AFX_MSG_MAP
	// Standard file based document commands
	ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
	ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
	// Standard print setup command
	ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CClientApp construction

CClientApp::CClientApp()
{
	dir[ 0 ] = '\0';
	::GetCurrentDirectory(1024, dir);

	m_pArchiveIn = NULL;
	m_pArchiveOut = NULL;
	m_pFile = NULL;
	m_pSocket = NULL;

	m_bConnection = FALSE;

	m_address = _T( "" );
	m_port = 0;
	m_name = _T( "" );
	m_type = 0;
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CClientApp object

CClientApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CClientApp initialization

BOOL CClientApp::InitInstance()
{
	if (!AfxSocketInit())
	{
		AfxMessageBox( "Socket init failed!" );
		return FALSE;
	}

	AfxEnableControlContainer();

	// Standard initialization
	// If you are not using these features and wish to reduce the size
	//  of your final executable, you should remove from the following
	//  the specific initialization routines you do not need.

#ifdef _AFXDLL
	Enable3dControls();			// Call this when using MFC in a shared DLL
#else
	Enable3dControlsStatic();	// Call this when linking to MFC statically
#endif

	// Change the registry key under which our settings are stored.
	// TODO: You should modify this string to be something appropriate
	// such as the name of your company or organization.
	SetRegistryKey(_T("miniSQL Client"));

	LoadStdProfileSettings();  // Load standard INI file options (including MRU)

	// Register the application's document templates.  Document templates
	//  serve as the connection between documents, frame windows and views.

	CMultiDocTemplate* pDocTemplate;
	pDocTemplate = new CMultiDocTemplate(
		IDR_CLIENTTYPE,
		RUNTIME_CLASS(CClientDoc),
		RUNTIME_CLASS(CChildFrame), // custom MDI child frame
		RUNTIME_CLASS(CClientView));
	AddDocTemplate(pDocTemplate);

	// add CHelpDoc and CHelpView into the new document
	AddDocTemplate(
		new CMultiDocTemplate(
		IDR_HELPTYPE,
		RUNTIME_CLASS(CHelpDoc),
		RUNTIME_CLASS(CChildFrame),
		RUNTIME_CLASS(CHelpView)) );

	// add CSelDoc and CSelView into the new document
	AddDocTemplate(
		new CMultiDocTemplate(
		IDR_SELTYPE,
		RUNTIME_CLASS(CSelDoc),
		RUNTIME_CLASS(CChildFrame),
		RUNTIME_CLASS(CSelView)) );

	// create main MDI Frame window
	CMainFrame* pMainFrame = new CMainFrame;
	if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
		return FALSE;
	m_pMainWnd = pMainFrame;

	// Enable drag/drop open
	m_pMainWnd->DragAcceptFiles();

	// Enable DDE Execute open
	EnableShellOpen();
	RegisterShellFileTypes(TRUE);

	// Parse command line for standard shell commands, DDE, file open
	CCommandLineInfo cmdInfo;
	ParseCommandLine(cmdInfo);

	// Dispatch commands specified on the command line
	if (!ProcessShellCommand(cmdInfo))
		return FALSE;

	// The main window has been initialized, so show and update it.
	pMainFrame->ShowWindow(m_nCmdShow);
	pMainFrame->UpdateWindow();

	return Logon();
}


/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	CHyperLink	m_hyperLink;
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	virtual BOOL OnInitDialog();
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	DDX_Control(pDX, IDC_URL_MAIL, m_hyperLink);
	//}}AFX_DATA_MAP
}

BOOL CAboutDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	m_hyperLink.SetURL(_T("mailto:luxiaochun_1111@163.com"));
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

// App command to run the dialog
void CClientApp::OnAppAbout()
{
	CAboutDlg aboutDlg;
	aboutDlg.DoModal();
}

/////////////////////////////////////////////////////////////////////////////
// CClientApp message handlers

CMsg* CClientApp::ReceiveMsg()
{
	static CMsg msg;

	TRY
	{
		msg.m_msgList.RemoveAll();
		msg.Serialize( *m_pArchiveIn );
	}
	CATCH( CFileException, e )
	{
		msg.m_bClose = TRUE;
		m_pArchiveOut->Abort();

		Message( "Server has reset the connection!\nClosing connection..." );
	}
	END_CATCH

	return &msg;
}

void CClientApp::ProcessPendingRead()
{
	CMsg* pMsg;
	do
	{
		pMsg = ReceiveMsg();

		if( pMsg->m_bClose )
		{
			delete m_pArchiveIn;
			m_pArchiveIn = NULL;
			delete m_pArchiveOut;
			m_pArchiveOut = NULL;
			delete m_pFile;
			m_pFile = NULL;
			delete m_pSocket;
			m_pSocket = NULL;

			m_bConnection = FALSE;
			CString strPort;
			strPort.Format( "%d", m_port );
			CString text;
			text += (CString)"Server (" + m_address + ":" + strPort
					+ ") has stopped services...\n";
			Message( text );
			return ;
		}

		switch( pMsg->m_cmdID )
		{
		case LOGON:
			{
				m_type = (DWORD)pMsg->m_nCount;
				if( !m_type )
				{
					AfxMessageBox( "用户不存在或密码错误!" );
					Logon();
				}
				else
					m_bConnection = TRUE;
			}
			break;
		case FILEBAR_INIT:
			{
				CMainFrame* pMainFrm = ( CMainFrame* )AfxGetMainWnd();
				POSITION pos = pMsg->m_msgList.GetHeadPosition();

				while( pos )
				{
					CString tname = pMsg->m_msgList.GetNext( pos );
					pMainFrm->m_FileBar.insert_tree( tname, pMainFrm->m_FileBar.hitem0 );
				}
				pMainFrm->m_FileBar.ExpandTree();
			}
			break;
		case FILEBAR_EXPAND:
			{
				HTREEITEM hItem = m_treeList.RemoveHead();
				CMainFrame* pMainFrm = ( CMainFrame* )AfxGetMainWnd();

				POSITION pos = pMsg->m_msgList.GetHeadPosition();
				for( int i = 0; i < pMsg->m_nCount; i++ )
				{
					CString attr;
					attr = pMsg->m_msgList.GetNext( pos );

					pMainFrm->m_FileBar.InsertAttr( attr, hItem );
				}

				while( pos )
				{
					CString index;
					index = pMsg->m_msgList.GetNext( pos );

					pMainFrm->m_FileBar.InsertIndex( index, hItem );
				}

				pMainFrm->m_FileBar.m_TreeCtrl.Expand( hItem, TVE_EXPAND );
			}
			break;
		case FAILED:
			{
				CClientDoc* pDoc = ( CClientDoc* )m_ClientDocList.RemoveHead();
				POSITION pos = pDoc->GetFirstViewPosition();
				CClientView* pView = ( CClientView* )pDoc->GetNextView( pos );
				ASSERT_VALID( pView );
				ASSERT_KINDOF( CClientView, pView );

				int nLine = pMsg->m_nCount;
				pView->SetSelection( CPoint( 0, nLine ), 
					CPoint( pDoc->m_TextBuffer.GetLineLength( nLine ), nLine ) );
				Message( pMsg->m_msgList.RemoveHead() );

				int id;
				::sscanf( (LPCTSTR)pMsg->m_msgList.GetHead(), "%d", &id );
				switch( id )
				{
				case OUT_TYPE_ERROR:
					{
						CSelDoc* pDoc = ( CSelDoc* )m_SelDocList.RemoveHead();
						ASSERT_VALID( pDoc );
						ASSERT_KINDOF( CSelDoc, pDoc );

						pDoc->OnCloseDocument();
					}
					break;
				default:
					;
				}
			}
			break;
		case SEL_START:
			{
				POSITION pos = GetFirstDocTemplatePosition();
				CDocTemplate* pTemplate;

				while( pos )
				{
					pTemplate = GetNextDocTemplate( pos );
					CString str;
					pTemplate->GetDocString( str, CDocTemplate::docName );
					if( str == _T( "SEL" ) )
					{
						pTemplate->OpenDocumentFile(NULL);
						break;
					}
				}

				pos = pTemplate->GetFirstDocPosition();
				CSelDoc* pDoc;
				while( pos )
					pDoc = ( CSelDoc* )pTemplate->GetNextDoc( pos );
				ASSERT_VALID( pDoc );
				ASSERT_KINDOF( CSelDoc, pDoc );
				m_SelDocList.AddTail( pDoc );

				Message( pMsg->m_msgList.GetHead() );
			}
			break;
		case SEL_COL:
			{
				CSelDoc* pDoc = ( CSelDoc* )m_SelDocList.GetHead();
				ASSERT_VALID( pDoc );
				ASSERT_KINDOF( CSelDoc, pDoc );

				POSITION pos = pDoc->GetFirstViewPosition();
				CSelView* pView = ( CSelView* )pDoc->GetNextView( pos );
				ASSERT_VALID( pView );
				ASSERT_KINDOF( CSelView, pView );

				pView->InsertCol( *pMsg );
			}
			break;
		case SEL_ITEM:
			{
				CSelDoc* pDoc = ( CSelDoc* )m_SelDocList.GetHead();
				ASSERT_VALID( pDoc );
				ASSERT_KINDOF( CSelDoc, pDoc );

				POSITION pos = pDoc->GetFirstViewPosition();
				CSelView* pView = ( CSelView* )pDoc->GetNextView( pos );
				ASSERT_VALID( pView );
				ASSERT_KINDOF( CSelView, pView );

				pView->InsertItem( *pMsg );
			}
			break;
		case SEL_SUCCESSFUL:
			{
				m_SelDocList.RemoveHead();

				Message( pMsg->m_msgList.GetHead() );
			}
			break;
		case COMPLETED:
			{
				m_ClientDocList.RemoveHead();
			}
			break;
		case TABLE_CREATED:
			{
				CString tname = pMsg->m_msgList.GetHead();
				
				CMainFrame* pMainFrm = ( CMainFrame* )AfxGetMainWnd();
				pMainFrm->m_FileBar.insert_tree( tname, pMainFrm->m_FileBar.hitem0 );
				pMainFrm->m_FileBar.ExpandTree();

				Message( "Successful : Created table \"" + tname + "\"\n" );
			}
			break;
		case INDEX_CREATED:
			{
				CString tname = pMsg->m_msgList.RemoveHead();
				CString iname = pMsg->m_msgList.RemoveHead();

				CMainFrame* pMainFrm = ( CMainFrame* )AfxGetMainWnd();
				pMainFrm->m_FileBar.InsertIndex( tname, iname );

				Message( "Successful : Created index \"" + iname + ".bpt\" on table \"" + tname + "\"\n" );
			}
			break;
		case TABLE_DROP:
			{
				CString tname = pMsg->m_msgList.GetHead();

				CMainFrame* pMainFrm = ( CMainFrame* )AfxGetMainWnd();
				pMainFrm->m_FileBar.DelFile( tname );

				Message( "Successful : Dropped table \"" + tname +"\"\n" );
			}
			break;
		case INDEX_DROP:
			{
				CString tname = pMsg->m_msgList.RemoveHead();
				CString iname = pMsg->m_msgList.RemoveHead();

				CMainFrame* pMainFrm = ( CMainFrame* )AfxGetMainWnd();
				pMainFrm->m_FileBar.DropIndex( tname, iname );

				Message( "Successful : Dropped index \"" + iname + ".bpt\" on table\"" + tname + "\"\n" );
			}
			break;
		case SUCCESSFUL:
			{
				Message( pMsg->m_msgList.GetHead() );
			}
			break;
		default:
			;
		}
	}
	while( !m_pArchiveIn->IsBufferEmpty() );
}


void CClientApp::SendMsg(CMsg &msg)
{
	if( m_pArchiveOut )
	{
		TRY
		{
			msg.Serialize( *m_pArchiveOut );
			m_pArchiveOut->Flush();
		}
		CATCH( CFileException, e )
		{
			m_pArchiveOut->Abort();
			delete m_pArchiveOut;
			m_pArchiveOut = NULL;

			Message( "Server has reset the connection!\n" );
		}
		END_CATCH
	}
}

BOOL CClientApp::ConnectSocket( LPCTSTR name, LPCTSTR code, LPCTSTR address, UINT port )
{
	m_pSocket = new CRecSocket();
	if( !m_pSocket->Create() )
	{
		delete m_pSocket;
		m_pSocket = NULL;
		AfxMessageBox( "Socket creating failed!" );
		return FALSE;
	}

	while( !m_pSocket->Connect( address, port + 700 ) )
	{
		if( AfxMessageBox( "无法连接,要重试吗?", 
				MB_ICONQUESTION | MB_YESNO ) == IDNO )
		{
			delete m_pSocket;
			m_pSocket = NULL;
			return FALSE;
		}
	}

	m_pFile = new CSocketFile( m_pSocket );
	m_pArchiveIn = new CArchive( m_pFile, CArchive::load );
	m_pArchiveOut = new CArchive( m_pFile, CArchive::store );

	m_name = name;
	m_address = address;
	m_port = port;

	CMsg msg( LOGON );
	msg.m_msgList.AddTail( name );
	msg.m_msgList.AddTail( code );
	SendMsg( msg );

	return TRUE;
}

void CClientApp::Drop()
{
	if( (m_pSocket != NULL) && (m_pFile != NULL) && (m_pArchiveOut != NULL) )
	{
		CMsg msg( CLIENT_CLOSE );
		msg.m_bClose = TRUE;
		msg.m_msgList.AddTail( m_name );
		msg.Serialize( *m_pArchiveOut );
		m_pArchiveOut->Flush();
	}

	delete m_pArchiveIn;
	m_pArchiveIn = NULL;
	delete m_pArchiveOut;
	m_pArchiveOut = NULL;
	delete m_pFile;
	m_pFile = NULL;

	if( m_pSocket )
	{
		BYTE buffer[ 50 ];
		m_pSocket->ShutDown();

		while( m_pSocket->Receive( buffer, 50 ) > 0 );
	}

	delete m_pSocket;
	m_bConnection = FALSE;
	m_pSocket = NULL;
	m_address = _T("");
	m_port = 0;
	m_name = _T("");
	m_type = 0;
}

BOOL CClientApp::Logon()
{
	CLogDlg dlg;
	dlg.m_address = m_address;
	dlg.m_port = m_port;
	dlg.m_name = m_name;

	if( dlg.DoModal() == IDOK )
	{
		if( m_pSocket && (dlg.m_address == m_address) && (dlg.m_port == m_port) )
		{
			CMsg msg( LOGON );
			msg.m_msgList.AddTail( dlg.m_name );
			msg.m_msgList.AddTail( dlg.m_code );
			SendMsg( msg );
			return TRUE;
		}
		else if( m_pSocket )
		{
			Drop();
			if( ConnectSocket( dlg.m_name, dlg.m_code, dlg.m_address, dlg.m_port ) )
				return TRUE;
		}
		else
		{
			if( ConnectSocket( dlg.m_name, dlg.m_code, dlg.m_address, dlg.m_port ) )
				return TRUE;
		}

		if ( AfxMessageBox( "要不要更改IP地址?",MB_ICONQUESTION | MB_YESNO) == IDYES )
			return Logon();
		else
			return FALSE;
	}
	else
		CWinApp::OnAppExit();

	return FALSE;
}

int CClientApp::ExitInstance() 
{
	Drop();
	
	return CWinApp::ExitInstance();
}

void CClientApp::OnConnect() 
{
	Logon();
}


void CClientApp::OnUpdateConnect(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable( !m_bConnection );	
}


void CClientApp::OnDisconnect() 
{
	if( AfxMessageBox( "这将断开与服务器的连接,\n要继续吗?",
		MB_ICONQUESTION | MB_YESNO ) == IDYES )
		Drop();
}

void CClientApp::OnUpdateDisconnect(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable( m_bConnection );	
}


void CClientApp::OnHelpMe() 
{
	CString path( theApp.dir );
	path += "\\help.hlp";
	OpenDocumentFile( path );
}

void CClientApp::OnFileNew() 
{
	POSITION curTemplatePos = GetFirstDocTemplatePosition();

	while( curTemplatePos != NULL )
	{
		CDocTemplate* curTemplate =
			GetNextDocTemplate( curTemplatePos );
		CString str;
		curTemplate->GetDocString( str, CDocTemplate::docName );
		if( str == _T( "Client" ) )
		{
			curTemplate->OpenDocumentFile(NULL);
			return;
		}
	}	
}